package cn.cangfei.massage.controller;


import cn.cangfei.massage.common.api.CommonResult;
import cn.cangfei.massage.dto.param.MassageCmsParam;
import cn.cangfei.massage.entity.MassageCms;

import cn.cangfei.massage.service.MassageCmsService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(value = "推拿馆模块", tags = "轮播图接口")
@RestController
@CrossOrigin
@RequestMapping("/massage")
public class MassageCmsController {


    @Resource
    private MassageCmsService massageCmsService;

    @ApiOperation(value = "添加轮播图信息", notes = "传入 轮播图信息参数（图片地址、排序）")
    @RequestMapping(value = "/cms/info", method = RequestMethod.POST)
    public CommonResult insertMassageCmsInfo(@RequestBody MassageCmsParam param) {


        if (massageCmsService.insert(param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }


    @ApiOperation(value = "查看轮播图信息", notes = "传入 轮播图信息参数（轮播图编号）")
    @RequestMapping(value = "/cms/info/{id}", method = RequestMethod.GET)
    public CommonResult<MassageCms> getMassageCmsInfo(@PathVariable("id") Long id) {


        try {
            if (massageCmsService.countCms(id)) {
                MassageCms massageCms = massageCmsService.getMassageCmsInfo(id);
                return CommonResult.success(massageCms);
            } else {
                return CommonResult.failed("该轮播图不存在！");
            }
        } catch (Exception e) {
            return CommonResult.failed("服务器错误，请联系管理员！");
        }


    }


    @ApiOperation(value = "更新轮播图信息", notes = "传入 轮播图编号、轮播图信息参数（名称，图片、地址，简介）")
    @ApiImplicitParam(name = "id", value = "轮播图编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/cms/info/{id}", method = RequestMethod.PUT)
    public CommonResult updateMassageCmsInfo(@PathVariable("id") Long id, @RequestBody MassageCmsParam param) {


        if (!massageCmsService.countCms(id)) {
            return CommonResult.validateFailed("不存在，该轮播图编号！");
        }

        if (massageCmsService.updateCms(id, param)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }


    @ApiOperation(value = "删除轮播图信息", notes = "传入 轮播图编号")
    @ApiImplicitParam(name = "id", value = "轮播图编号", paramType = "path", dataType = "Long", required = true)
    @RequestMapping(value = "/cms/info/{id}", method = RequestMethod.DELETE)
    public CommonResult deleteMassageInfo(@PathVariable("id") Long id) {


        if (!massageCmsService.countCms(id)) {
            return CommonResult.validateFailed("不存在，该轮播图编号！");
        }

        if (massageCmsService.deleteCms(id)) {
            return CommonResult.success();
        }

        return CommonResult.failed("服务器错误，请联系管理员！");
    }


    @ApiOperation(value = "分页：搜索轮播图信息", notes = "传入 推拿馆名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "轮播图名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "第几页", paramType = "query", dataType = "Integer",
                    required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer",
                    required = true)
    })
    @RequestMapping(value = "/cms/info/list", method = RequestMethod.GET)
    public CommonResult<IPage<MassageCms>> searchMassageCmsInfo(@RequestParam(required = false) String name,
                                                                  @RequestParam Integer pageNum,
                                                                  @RequestParam Integer pageSize) {

        IPage<MassageCms> list = massageCmsService.listWithQuery(name, pageNum, pageSize);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "查询全部轮播图信息", notes = "")
    @RequestMapping(value = "/cms/info/listAll", method = RequestMethod.GET)
    public CommonResult<List<MassageCms>> searchMassageCmsInfoList() {

        List<MassageCms> list = massageCmsService.searchMassageCmsInfoList();
        return CommonResult.success(list);
    }


}
